package com.jamie.springdemo;

import com.jamie.springdemo.business.controller.WelComeController;
import com.jamie.springdemo.business.entity.User;
import com.jamie.springdemo.business.service.WelComeService;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.support.FileSystemXmlApplicationContext;

@Configurable
@ComponentScan("com.jamie")
public class Entrance {

    public static void main1(String[] args) {
        System.out.println("HelloWorld");
        // 配置文件路径
        String xmlPath = "E:\\jamie-product\\personal\\jamie-spring-framework\\spring-demo\\src\\main\\resources"
                + "\\spring\\spring-conf.xml";
        // 通过配置文件，获取上下文
        FileSystemXmlApplicationContext applicationContext = new FileSystemXmlApplicationContext(xmlPath);
        // 通过上下文对象以及bean的名称，获取bean的实例
        WelComeService welComeService = (WelComeService)applicationContext.getBean("welComeService");
        // 调用bean实例的方法
        String result = welComeService.sayHello("强大的Spring框架");
        System.out.println(result);
    }

    public static void main(String[] args) {
        // 通过注解，获取上下文
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Entrance.class);
        String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
        for(String beanDefinitionName: beanDefinitionNames){
            System.out.println(beanDefinitionName);
        }
        // 通过上下文对象以及bean的名称，获取bean的实例
        WelComeService welComeService = (WelComeService)applicationContext.getBean("welComeServiceImpl");
        // 调用bean实例的方法
        String result = welComeService.sayHello("强大的Spring框架");
        System.out.println(result);
        // 通过上下文对象以及bean的名称，获取bean的实例
        WelComeController welComeController = (WelComeController)applicationContext.getBean("welComeController");
        // 调用bean实例的方法
        welComeController.handleRequest();

        User user5 = (User)applicationContext.getBean("user5");
        System.out.println("CustomizedBeanDefinitionRegistryPostProcessor创建的对象：" + user5);

    }

}
